{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">编辑商品</div>
    <div class="layui-card-body">
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <input value="{$data.goodsname}" type="text" style="width: 500px;" name="goodsname" lay-verify="required" autocomplete="off" placeholder="商品名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品类别</label>
                <div class="layui-input-inline">
                    <select name="categoryid" lay-verify="required">
                        {volist name="categorys" id="vo"}
                        <option value="{$vo['id']}" {eq name="$data.id" value="$vo['id']" }selected{/eq} >{$vo['categoryname']}</option>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品价格</label>
                <div class="layui-input-inline">
                    <input value="{$data.money}" type="text" style="width: 500px;" name="money" lay-verify="required" autocomplete="off" placeholder="商品价格" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">促销价格</label>
                <div class="layui-input-inline">
                    <input value="{$data.promotionmoney}" type="text" style="width: 500px;" name="promotionmoney" lay-verify="required" autocomplete="off" placeholder="促销价格" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品状态</label>
                <div class="layui-input-inline">
                    <select name="state" lay-verify="required">
                        <option value="0" {eq name="$data.state" value="0" >正常</option>
                        <option value="1" {eq name="$data.state" value="1" >缺货</option>
                        <option value="2" {eq name="$data.state" value="2" >不销售</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <input value="{$data.image}" type="hidden" id="goodsimagestr" style="width: 500px;" name="image" lay-verify="required" autocomplete="off" placeholder="商品图片" class="layui-input">
                <label class="layui-form-label">商品图片</label>
                <div class="layui-input-inline">
                    <div class="avatar-add">
                        <button type="button" class="layui-btn upload-img"> <i class="layui-icon"></i>上传头像 </button><input class="layui-upload-file" type="file" accept="undefined" name="file">
                        <img onclick="openImage()" id="goodsimage" src="{$data.image}" style="width: 100px;height: 100px;"> <span class="loading"></span>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品简介</label>
                <div class="layui-input-inline">
                    <textarea name="introduce" style="width: 500px;height: 100px;" placeholder="商品简介" class="layui-textarea">{$data.introduce}</textarea>
                </div>
            </div>
            <input type="hidden" name="id" value="{$data.id}">
            <div class="layui-form-item">
                <div>
                    <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
                    <button class="layui-btn layui-btn-normal" type="button" onclick="javascript:history.back(-1);">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}

{block name="script"}
<script>
    //全局变量
    var GV = {
        'image_upload_url': '{$image_upload_url ? $image_upload_url : url("attachment/upload/upload", ["dir" => "images", "module" => request()->module()])}',
        'profile_upload_url': '{$profile_upload_url ? $profile_upload_url : url("machine/goods/profile")}',
        'image_url' : '{url("machine/goods/profile")}'
    };

    //上传图片
    if ($('.upload-img')[0]) {
        layui.use('upload', function(upload) {
            var avatarAdd = $('.avatar-add');
            upload.render({
                elem: '.upload-img',
                url: GV.image_upload_url,
                size: 1024,
                before: function() {
                    avatarAdd.find('.loading').show();
                },
                done: function(res) {
                    //console.log(res);
                    if (res.code == 0) {
                        document.getElementById("goodsimage").src = res.path;
                        document.getElementById("goodsimagestr").value = res.path;

                        console.log(res.path)

                        /*$.post(GV.profile_upload_url, {
                            avatar: res.path
                        }, function(res) {
                            //location.reload();
                        });*/
                    } else {
                        layer.msg(res.info, { icon: 5 });
                    }
                    avatarAdd.find('.loading').hide();
                },
                error: function() {
                    avatarAdd.find('.loading').hide();
                }
            });
        });
    }

    function openMsg(data) {
        var data = '<img id="pitureChange" style="width: 100%;height: 100%" src="' + data + '"  >'
        layer.msg(data, {
            time: 1000
        });
    }

    function openImage() {
        var imgstr = document.getElementById("goodsimage").src;

        openMsg(imgstr);
    }

</script>
{/block}